package com.ng.common.log.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ng.common.log.annotation.SysLog;
import com.ng.common.log.entity.BookEntity1;
import com.ng.common.log.entity.ViewDownloadEntity;
import com.ng.common.log.service.DownLogService;
import com.ng.common.utils.HttpContextUtils;
import com.ng.common.utils.IPUtils;
import com.ng.common.utils.ReflectUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 系统日志，切面处理类
 * 
 * @author lyf
 * @email  
 * @date 2017年3月8日 上午11:07:35
 */
@Aspect
@Component
public class SearchLogAspect {
	@Autowired
	private DownLogService downLogService;
	
  
	@Before("@annotation(search)")
	public void around(JoinPoint point , SysLog search) throws Throwable {
		saveSysLog(point);
	}

	private void saveSysLog(JoinPoint joinPoint) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();

		ViewDownloadEntity sysLog = new ViewDownloadEntity();
		SysLog syslog = method.getAnnotation(SysLog.class);
//		if(syslog != null){
//			//注解上的描述
//			sysLog.setOperation(syslog.value());
//			if(syslog.system() != null)
//				sysLog.setSystem(syslog.system().getName());
//
//		}

		//请求的方法名
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = signature.getName();
		//sysLog.setMethod(className + "." + methodName + "()");
		
		// 回填模块
		

		//请求的参数
		Object[] args = joinPoint.getArgs();
		String[] paramNames = signature.getParameterNames();
		try{
			
//			String params = "" ;
 			JSONObject jo = new JSONObject();
			for(int i = 0 ; i < paramNames.length && i < args.length ; i++) {
				String pname = paramNames[i];
				Object value = args[i];
				
				if(value instanceof MultipartFile) {
					Map<String, Object> p_map = new HashMap<>();
					MultipartFile file = (MultipartFile)value;
					p_map.put("filename", file.getOriginalFilename());
					p_map.put("file_size", file.getSize());
					
					value = p_map;
				}

				String tempValue = JSON.toJSONString(value) ;//value.toString();
				BookEntity1  bb= JSON.parseObject(tempValue, BookEntity1.class);


				sysLog.setResource_title(bb.书名);
				sysLog.setResource_id(bb.id);
				sysLog.setResource_name(bb.filename);
			}
		
			
			String params = jo.toJSONString();
			sysLog.setResource_title(params);
		}catch (Exception e){
			System.out.println(e.getMessage());
		}

		//获取request
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		//设置IP地址
		sysLog.setIp(IPUtils.getIpAddr(request));

		Subject subject = SecurityUtils.getSubject();
		
		Object user = subject.getPrincipal();
		if(user!=null) {
			//用户名
			Object username = ReflectUtils.getFieldValue(user, "userNo");
			if (username != null)
				sysLog.setUser_name(username.toString());
		}
		//sysLog.setTime(time);
		sysLog.setCreate_time(new Date());

		//保存系统日志
		downLogService.save(sysLog);
	}
}
